function AbsPopupBox() {
};

AbsPopupBox.gui = function(unique) {
  return { title: document.getElementById(unique + '_title_img'),
           menu: document.getElementById(unique + '_menu_div'),
           panel: document.getElementById(unique + '_panel_div'),
           close: document.getElementById(unique + '_close_button')
         };
};

function PopupBox(doc, container, width, height, user, date, options) {
  this._doc = doc;
  this._container = container;
  this._width = (width) ? width : '513';
  this._height = (height) ? height: '416';
  this._user = user;
  this._date = date;
  this._options = options;
  this._unique = PopupBox.unique();
  
  this._createElements();
  this._gui = AbsPopupBox.gui(this._unique);
}

PopupBox.prototype._createElements = function() {
  var table, tr, td, input;
  table = this._doc.createElement('table');
  table.style.width = this._width + 'px';
  table.style.height = this._height + 'px';
  table.border=0
  table.style.margin='20px 0 0 0'
  table.cellPadding = 0;
  table.cellSpacing = 0; 
  this._container.appendChild(table);
  
  tr=table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = '2px';
  td.style.width = '2px';
  td.style.backgroundImage='url(image/popupbox/tl.png)';
  td.style.backgroundRepeat = 'no-repeat';
  td = tr.insertCell(-1);
  td.style.height = '2px';
  td.style.width = (this._width-8)+'px';
  td.style.backgroundImage='url(image/popupbox/tm.png)';
  td.style.backgroundRepeat = 'repeat-x';
  td = tr.insertCell(-1);
  td.style.height = '2px';
  td.style.width = '6px';
  td.style.backgroundImage='url(image/popupbox/tr.png)';
  td.style.backgroundRepeat = 'no-repeat';
  
  tr=table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = (this._height-8) + 'px';
  td.style.width = '2px';
  td.style.backgroundImage='url(image/popupbox/lm.png)';
  td.style.backgroundRepeat = 'repeat-y';
  td = tr.insertCell(-1);
  td.style.height = (this._height-8) + 'px';
  td.style.width = (this._width-8) + 'px';
  td.style.background = '#f0f0f0 url(image/popupbox/back.png) repeat-x 0 0';
    
  var itable = this._doc.createElement('table');
  itable.style.width = (this._width-8) + 'px';
  itable.style.height = (this._height-8) + 'px';
  itable.cellPadding = 0;
  itable.cellSpacing = 0; 
  itable.border = 0
  itable.style.margin='-1px -3px 0 -1px';
  td.appendChild(itable);
  td = tr.insertCell(-1);
  td.style.height = (this._height-8) + 'px';
  td.style.width = '6px';
  td.style.textAlign = 'left'
  td.style.backgroundImage='url(image/popupbox/rm.png)';
  td.style.backgroundRepeat = 'repeat-y';
  
  tr=table.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.height = '6px';
  td.style.width = '2px';
  td.style.backgroundImage='url(image/popupbox/bl.png)';
  td.style.backgroundRepeat = 'no-repeat';
  td = tr.insertCell(-1);
  td.style.height = '6px';
  td.style.width = (this._width-8) + 'px';
  td.style.backgroundImage='url(image/popupbox/bm.png)';
  td.style.backgroundRepeat = 'repeat-x';
  td = tr.insertCell(-1);
  td.style.height = '6px';
  td.style.width = '6px';
  td.style.backgroundImage='url(image/popupbox/br.png)';
  td.style.backgroundRepeat = 'no-repeat';


  itr = itable.insertRow(-1);
  itd = itr.insertCell(-1);
  itd.style.height = '29px';
  itd.style.width = (this._width-6) + 'px';
 // itd.align = 'center';
  itd.id=this._unique + '_menu_div';
  var table1 = this._doc.createElement('table');
  table1.style.width = (this._width-6) + 'px';
  table1.style.height = '29px';
  table1.cellPadding = 0;
  table1.cellSpacing = 0;
//  table1.border=1
  itd.appendChild(table1);
  
  tr1 = table1.insertRow(-1);
  td1 = tr1.insertCell(-1);
  td1.style.height = '29px';
  td1.style.width = '180px';
  td1.style.padding='0 0 0 11px'
  td1.style.whiteSpace = 'nowrap'
  td1.align = 'left';
  td1.id = this._unique + '_title_img';
  td1.style.fontFamily = 'Trebuchet MS';
  td1.style.fontSize = '14px';
  td1.style.fontWeight = 'bold';
  td1.style.color = '#000';
  td1.style.backgroundImage='url(image/popupbox/ttl.png)';
  td1.style.backgroundRepeat = 'no-repeat';
  
  td1 = tr1.insertCell(-1);
  td1.style.height = '29px';
  td1.style.width = (this._width - 240) + 'px';
  td1.style.backgroundImage='url(image/popupbox/ttm.png)';
  td1.style.backgroundRepeat = 'repeat-x';
  
  td1 = tr1.insertCell(-1);
  td1.style.height = '29px';
  td1.align = 'left';
  td1.style.verticalAlign='top';
  td1.style.textAlign = 'right';
  td1.style.paddingRight = '15px';
  td1.style.backgroundImage='url(image/popupbox/ttr.png)';
  td1.style.backgroundRepeat = 'no-repeat';
  var close = this._doc.createElement('img');
  close.style.cursor = 'pointer';
  close.src = 'image/popupbox/close.png';
  close.id = this._unique + '_close_button';
  td1.appendChild(close);
  
  
  itr=itable.insertRow(-1);
  itd = itr.insertCell(-1);
  itd.style.width = (this._width-8) + 'px';
  itd.style.height = (this._height-8-29) + 'px';
  itd.style.verticalAlign = 'top';
  itd.align = 'left';
  itd.id = this._unique + '_panel_div';
  itd.style.backgroundColor = '#ffffff';
};

PopupBox.unique = function() {
  var unique = 'u' + parseInt(new Date().getTime() * Math.random());
  unique = unique.replace('.', '');
  unique.substring(0, 16);
  return unique;
};

function ModulePopupBox() {
  this._background = null;
  this._container = null;
}

ModulePopupBox.prototype._reset = function(pos) {
  this._close();
  
  var size = MyUtils.xDocSize();
  this._background = document.createElement('div');
  this._background.style.background = 'url(image/pop/bw.png) repeat scroll center top transparent';
  this._background.style.position = 'absolute';
  this._background.style.top = 0;
  this._background.style.left = 0;
  this._background.style.width = size.w + 'px';
  this._background.style.height = size.h + 'px';
  this._background.style.zIndex = '999';
  document.body.appendChild(this._background);
  
  this._container = document.createElement('div');
  this._container.style.position = 'absolute';
  this._container.style.top = ((this._position[1] - 60) > 0) ? (this._position[1] - 60) + 'px' : '0px';
  this._container.style.left = ((this._position[0] + 200) > window.screen.width) ? (window.screen.width - 200) + 'px' : this._position[0] + 'px';
  this._background.appendChild(this._container);

  var posX = this._position[0];
  var posY = ((this._position[1] - 100) > 0) ? (this._position[1] - 100) : 0;
  //window.scrollTo(posX, posY);
};

ModulePopupBox.prototype._open = function(container, options) {
  var table, tr, td, _self = this;

  this._title = (options && options.t) ? options.t : '';
  
  this._position = (options && options.pos) ? options.pos : [((document.body.offsetWidth / 2) - 50), ((document.body.offsetHeight / 2) - 50)];
  
  this._reset();
  
  this._gui = new PopupBox(document, this._container, options.width, options.height, null, null, options)._gui;
  this._callbackFunc = (options && options.func) ? options.func : null;
  
  this._gui.title.appendChild(document.createTextNode(this._title));
  this._gui.close.onclick = function() { _self._close(); 
                                         if (_self._callbackFunc) {
                                           _self._callbackFunc();
                                         }
                                       };
  this._gui.panel.appendChild(container);
  
  this._moveX = 0;
  this._moveY = 0;
  this._moveTop = 0;
  this._moveLeft = 0;
  this._moveable = false;
  this._docMouseMoveEvent = document.onmousemove;
  this._docMouseUpEvent = document.onmouseup;
  this._iWidth = document.body.clientWidth;
  this._iHeight = document.body.clientHeight;
  this._width = (options && options.width) ? options.width : 513;

  if (options && options.movable == 0) {
    return;
  }
  this._gui.menu.onmouseover = function() { this.style.cursor = 'move'; };
  this._gui.menu.onmousedown = function() { var evt = _self._getEvent();
                                            _self._moveable = true;
                                            _self._moveX = evt.clientX;
                                            _self._moveY = evt.clientY;
                                            _self._moveTop = parseInt(_self._container.style.top);
                                            _self._moveLeft = parseInt(_self._container.style.left);
                                          };
  this._gui.menu.onmousemove = function() { if (_self._moveable) {
                                              this.style.cursor = 'move';
                                              var evt = _self._getEvent();
                                              var x = parseInt(_self._moveLeft) + parseInt(evt.clientX) - parseInt(_self._moveX);
                                              var y = parseInt(_self._moveTop) + parseInt(evt.clientY) - parseInt(_self._moveY);
                                              if ( x > 0 && ( x + _self._width < _self._iWidth) && y > 0 && (y + 300 < _self._iHeight) ) {
                                                _self._container.style.left = x + "px";
                                                _self._container.style.top = y + "px";
                                              } else {
                                                _self._container.style.left = x + "px";
                                                _self._container.style.top = y + "px";
                                              }
                                            }
                                          };
  this._gui.menu.onmouseup = function () { if (_self._moveable) {
                                             document.onmousemove = _self._docMouseMoveEvent;
                                             document.onmouseup = _self._docMouseUpEvent;
                                             _self._moveable = false;
                                             _self._moveX = 0;
                                             _self._moveY = 0;
                                             _self._moveTop = 0;
                                             _self._moveLeft = 0;
                                             this.style.cursor = 'handle';
                                           }
                                         };
  
  this._background.onmousemove = function() { if (_self._moveable) {
                                             var evt = _self._getEvent();
                                             var x = parseInt(_self._moveLeft) + parseInt(evt.clientX) - parseInt(_self._moveX);
                                             var y = parseInt(_self._moveTop) + parseInt(evt.clientY) - parseInt(_self._moveY);
                                             
                                             if ( x > 0 && ( x + _self._width < _self._iWidth) && y > 0 && (y + 300 < _self._iHeight) ) {
                                               _self._container.style.left = x + "px";
                                               _self._container.style.top = y + "px";
                                             } else {
                                               _self._container.style.left = x + "px";
                                               _self._container.style.top = y + "px";
                                             }
                                           }
                                         };
  
  this._background.onmouseup = function () { if (_self._moveable) {
                                            document.onmousemove = _self._docMouseMoveEvent;
                                            document.onmouseup = _self._docMouseUpEvent;
                                            _self._moveable = false;
                                            _self._moveX = 0;
                                            _self._moveY = 0;
                                            _self._moveTop = 0;
                                            _self._moveLeft = 0;
                                            this.style.cursor = 'handle';
                                          }
                                        };
};

ModulePopupBox.prototype._close = function() {
  if (this._background) {
    document.body.removeChild(this._background);
    this._background = null;
    this._container = null;
  }
};


ModulePopupBox.prototype._getEvent = function() {
  if(window.parent == window) {
    return window.event || arguments.callee.caller.arguments[0];
  } else {
    return window.parent.event || arguments.callee.caller.arguments[0];
  }
};
